package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.TagsBean;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.activity.story.StoryDetailsActivity;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.util.TagsUtil;

public class NovelMessageViewHolder extends BaseAdapterViewHolder implements OnClickListener {
    public TagsBean[] titleLabels;
    // 数据
    MineNovelBean bean;
    Handler mHandler;
    private ImageView ivPic;
    private TextView tvTitle,tvDesc,tvAuthorDesc,tvState,tvTag1,tvTag2;
    private View lay;
    private int singlePage;
    private String keyword;


    public NovelMessageViewHolder(Context context, int position) {
        super(context, position, R.layout.adapter_search_novel_item);
    }


    @Override
    public void getChildView(View convertView) {
        // TODO Auto-generated method stub
        lay = convertView.findViewById(R.id.lay);
        ivPic = convertView.findViewById(R.id.ivPic);
        tvTitle = convertView.findViewById(R.id.tvTitle);
        tvDesc = convertView.findViewById(R.id.tvDesc);
        tvAuthorDesc = convertView.findViewById(R.id.tvAuthorDesc);
        tvState = convertView.findViewById(R.id.tvState);
        tvTag1 = convertView.findViewById(R.id.tvTag1);
        tvTag2 = convertView.findViewById(R.id.tvTag2);
        lay.setOnClickListener(this);
    }

    @Override
    public void setChildView(Object object,int singlePage) {
        try {
            bean = (MineNovelBean) object;
            handleViewDisplay(bean,singlePage);
            this.singlePage = singlePage;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断是否为别名
     * @param keyword
     * @param aliases
     * @return
     */
    private boolean isAliases(String keyword, String[] aliases) {
        if (aliases == null) {
            return false;
        }
        if (keyword == null || keyword.isEmpty()) {
            return false;
        }
        for (int i = 0; i < aliases.length; i++) {
            String alia = aliases[i];
            if (keyword.equals(alia)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void setChildView(Object object, String keyWord,int singlePage) {
        keyword = keyWord;
        bean = (MineNovelBean) object;
        this.singlePage = singlePage;
        handleViewDisplay(bean,singlePage);
        /*-----------------关键字变红---------------*/
        // 小说标签
        if (bean.getTags() != null && bean.getTags().length > 0) {
            titleLabels = bean.getTags();
            String[] tagStrings = TagsUtil.convertTagbeans2Strings(titleLabels);
            if (tagStrings != null && tagStrings.length > 0) {
                tvTag1.setVisibility(View.VISIBLE);
                tvTag1.setText(DiaobaoUtil.textLighterColor(tagStrings[0],keyWord));
                if (tagStrings.length > 1) {
                    tvTag2.setVisibility(View.VISIBLE);
                    tvTag2.setText(DiaobaoUtil.textLighterColor(tagStrings[1],keyWord));
                }else{
                    tvTag2.setVisibility(View.GONE);
                }
            }else{
                tvTag1.setVisibility(View.GONE);
            }
        }else{
            tvTag1.setVisibility(View.GONE);
            tvTag2.setVisibility(View.GONE);
        }
        if (isAliases(keyWord, bean.getAliases())) {
            // 小说标题
            tvTitle.setText(DiaobaoUtil.textLighterColor(keyWord, keyWord));
            // 小说简介
            StringBuilder descSB = new StringBuilder();
            descSB.append("又名：").append(bean.getTitle()).append(" ").append(bean.getDes());
            tvDesc.setText(descSB);
        }else{
            // 小说标题
            tvTitle.setText(DiaobaoUtil.textLighterColor(bean.getTitle(), keyWord));
            // 小说简介
            tvDesc.setText(DiaobaoUtil.textLighterColor(StringUtil.filterLineFeed(bean.getDes()), keyWord));
        }
        // 作者名
        if (bean.getAuthor() != null && !bean.getAuthor().equals("")) {
            tvAuthorDesc.setText(DiaobaoUtil.textLighterColor(bean.getAuthor(), keyWord));
        } else {
            tvAuthorDesc.setText("佚名");
        }
    }





    private void handleViewDisplay(final MineNovelBean bean,int singlePage){
        this.bean = bean;
        if (bean.getTags() != null && bean.getTags().length > 0) {
            titleLabels = bean.getTags();
            final String[] tagStrings = TagsUtil.convertTagbeans2Strings(titleLabels);
            if (tagStrings != null && tagStrings.length > 0) {
                tvTag1.setVisibility(View.VISIBLE);
                tvTag1.setText(tagStrings[0]);
                tvTag1.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TagsUtil.openWeexTagActivity(mContext,tagStrings[0],Constant.OBJECT_TYPE_NOVEL,singlePage);
                    }
                });
                if (tagStrings.length == 1) {
                    tvTag2.setVisibility(View.GONE);
                }else{
                    tvTag2.setText(tagStrings[1]);
                    tvTag2.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TagsUtil.openWeexTagActivity(mContext,tagStrings[1],Constant.OBJECT_TYPE_NOVEL,singlePage);
                        }
                    });
                    tvTag2.setVisibility(View.VISIBLE);
                }
            } else {
                tvTag1.setVisibility(View.GONE);
                tvTag2.setVisibility(View.GONE);
            }
        }

        tvTitle.setText(bean.getTitle());

        // 作者名
        if (bean.getAuthor() != null && !bean.getAuthor().equals("")) {
            tvAuthorDesc.setText(bean.getAuthor());
        } else {
            tvAuthorDesc.setText("佚名");
        }
        // 简介
        tvDesc.setText(bean.getDes().replaceAll("\\s+", ""));
        if (singlePage == 1) {
            tvState.setVisibility(View.GONE);
            ivPic.setVisibility(View.GONE);

        }else{
            // 上传者图
            String imageUrl = "";
            if (bean.getImages() != null && bean.getImages().length > 0) {
                imageUrl = bean.getImages()[0].getImgUrl();
            }
            GlideUtil.displayRoundImage(ivPic,imageUrl,2,R.drawable.img_default_loading_l);
            if (isSendAtricle(bean)) {
                tvState.setVisibility(View.VISIBLE);
                if ("1".equals(bean.getFinished())) {
                    tvState.setText("完结");
                    tvState.setTextColor(ContextCompat.getColor(mContext,R.color.btn_green_noraml));
                }else{
                    tvState.setText("连载");
                    tvState.setTextColor(ContextCompat.getColor(mContext,R.color.text_color_8993A3));
                }
            } else {
                tvState.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    public void setHandler(Handler handler) {
        this.mHandler = handler;
    }

    // 判断是否有章节
    private boolean isSendAtricle(MineNovelBean bean) {
        return (bean.getStatus() != null && bean.getStatus().equals("1"));

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lay:
                if (bean != null) {
                    // 是本人
                    if (bean.getStatus().equals("0") && AccountService.getInstance().isUserSelf(bean.getUser().getUserId())) {
                        NovelCreateServices.enterNovelCreateList(mContext, bean.getId(), bean.getTitle());
                    } else {
                        if (singlePage == 1) {
                            StoryDetailsActivity.Companion.startActivity(mContext,Integer.parseInt(bean.getId()),Intent.FLAG_ACTIVITY_NEW_TASK);
                        }else{
                            NovelDetailsActivity.Companion.startActivity(mContext,Integer.parseInt(bean.getId()),keyword,Intent.FLAG_ACTIVITY_NEW_TASK);
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public BaseAdapterViewHolder createViewHolder(Context context, int position) {
        // TODO Auto-generated method stub
        return new NovelMessageViewHolder(context, position);
    }



}
